﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using RooHui.Tool;
using RooHui.Infrastructure.Configuration;
using RooHui.Infrastructure.Component.ShortMessage;

namespace RooHui.Infrastructure.Component.WorkPool.WorkHandle.ShortMessage
{
    internal abstract class BaseShortMessageHandle<T> : IWorkHandle<T>
    {
        string partition = string.Empty;
        protected virtual void Prepare(ShortMessageContext ctx, string partition = "common")
        {
            this.partition = partition;
            var accountItem = ConfigReader.WorkPool.ShortMessageQueue.Pool[partition].AccountList.RandomGet();
            ctx.CredentialPassword = accountItem.Password;
            ctx.CredentialUserName = accountItem.UserName;
            ctx.Tail = accountItem.Tail;
        }
        protected void SendShortMessageCallBack(ShortMessageContext context)
        {
            if (ConfigReader.WorkPool.ShortMessageQueue.Pool[this.partition].Enable)
            {
                new ShortMessageHandle(context).Send();
            }
        }

        #region IWorkHandle<T> 成员

        public abstract WaitWorkItem Process(T context);

        #endregion
    }

    internal class CommonShortMessageHandle<T> : BaseShortMessageHandle<T>
    {
        public override WaitWorkItem Process(T context)
        {
            NormalMessage shortmessagectx = context as NormalMessage;
            WaitWorkItem data = new WaitWorkItem();
            var ctx = new ShortMessageContext();
            ctx.Recipient = shortmessagectx.Recipient;
            Prepare(ctx);
            ctx.Body = String.Format("{0} 【{1}】", shortmessagectx.Message, ctx.Tail);
            data.Context = ctx;

            #region 赋值Action
            data.Action = o => { SendShortMessageCallBack(o as ShortMessageContext); };
            #endregion

            return data;
        }
    }
    
}
